package com.haidechizi.i18n.code.generator.common.message.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 文件操作类
 */
public class FileUtils {

    public static void loadFile(String path, List<String> filePaths) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                loadFile(file1.getPath(), filePaths);
            }
        } else {
            filePaths.add(path);
        }
    }

    public static String readFile1(String path) {
        StringBuilder sb = new StringBuilder();
        File file = new File(path);
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                sb.append(text).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static String readFile(String path) {
        return readFile(path, "utf-8");
    }

    public static String readFile(String path, String charsetName) {
        StringBuilder sb = new StringBuilder();
        File file = new File(path);
        try (FileInputStream fis = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(fis)) {
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, length, charsetName));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String path = "D:\\translate\\properties\\zh_HK.js";
//        String regex = "handle[\\s]*=[\\s]*(\\{[.*\n]+})";
        String regex = "handle[\\s]*=[\\s]*(\\{[\\s\\S]*});";
//        String regex = "(\n)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(readFile(path));
        if (matcher.find()) {
            String content = matcher.group(1);
            Map<String, String> map = JSON.parseObject(content, Map.class);
            System.out.println(map);
        }
    }


    public static void copyFile(String path, String suffix) {
        File file = new File(path);
        try (FileInputStream fileInputStream = new FileInputStream(file);
             FileOutputStream fileOutputStream = new FileOutputStream(new File(path + suffix))
        ) {
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, length);
            }
        } catch (Exception e) {

        }
    }

    public static void createFile1(String content, String path, String suffix) {
        try (FileWriter fw = new FileWriter(path + suffix)) {
            fw.write(content);
        } catch (Exception e) {

        }
    }

    public static void createFile(String content, String path, String suffix) {
        try (FileOutputStream fos = new FileOutputStream(path + suffix);
             OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");) {
            osw.write(content);
        } catch (Exception e) {

        }
    }


    /**
     * 将配置文件中的Unicode 转 utf-8 汉字
     *
     * @param str 原始字符串
     * @return 转换后的格式的字符串
     */
    public static String unicodeToChina(String str) {
        Charset set = Charset.forName("UTF-16");
        Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
        Matcher m = p.matcher(str);
        int start = 0;
        int start2 = 0;
        StringBuffer sb = new StringBuffer();
        while (m.find(start)) {
            start2 = m.start();
            if (start2 > start) {
                String seg = str.substring(start, start2);
                sb.append(seg);
            }
            String code = m.group(1);
            int i = Integer.valueOf(code, 16);
            byte[] bb = new byte[4];
            bb[0] = (byte) ((i >> 8) & 0xFF);
            bb[1] = (byte) (i & 0xFF);
            ByteBuffer b = ByteBuffer.wrap(bb);
            sb.append(String.valueOf(set.decode(b)).trim());
            start = m.end();
        }
        start2 = str.length();
        if (start2 > start) {
            String seg = str.substring(start, start2);
            sb.append(seg);
        }
        return sb.toString();
    }

    public static String chinaToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (isChinese(c) || isChinesePunctuation(c)) {
                sb.append("\\u").append(Integer.toString(c, 16));
            } else {
                sb.append(c);

            }
        }
        return sb.toString();
    }

    //使用UnicodeScript方法判断
    public static boolean isChinese(char c) {
        Character.UnicodeScript sc = Character.UnicodeScript.of(c);
        if (sc == Character.UnicodeScript.HAN) {
            return true;
        }
        return false;
    }

    // 根据UnicodeBlock方法判断中文标点符号
    public static boolean isChinesePunctuation(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
                || ub == Character.UnicodeBlock.VERTICAL_FORMS) {
            return true;
        }
        return false;
    }

}
